package pom_Smp_PO;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.io.FileReader;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class OpenDriver {
    public WebDriver driver;
    Properties properties = new Properties();

    /**
     * 启动浏览器
     * @throws Exception
     */
    public  void Starbrowser() throws Exception {
        //读取Properties文件的内容
        File file = new File("file.input/GlobalData.properties");

        //判断File文件是否存在
        if (file.exists()) {
            properties.load(new FileReader(file));
        } else {
            file.createNewFile();
        }

        //获取配置文件里的browser
        String browser = properties.getProperty("browser");

        if (browser.equals("Firefox")) {
            System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver.exe");
            //打开浏览器
            driver = new FirefoxDriver();

        } else if (browser.equals("Google")) {
            System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
            //打开浏览器
            driver = new ChromeDriver();
        }
//        else if (browser.equals("Ie")) {
//            System.setProperty("", "");
//            //打开浏览器
//            WebDriver driver = new InternetExplorerDriver();
//        }

        //获取配置中的url
        String url = properties.getProperty("url");
        driver.get(url);

        driver.manage().window();
        //隐式等待10秒  如果页面加载慢就等待页面加载
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
}
